package cn.jobstart.spider.pageProcessor;

import cn.jobstart.common.spider.Job51Util;
import cn.jobstart.common.StringUtil;
import cn.jobstart.enums.TalentSubscriptionTaskChannelEnum;
import cn.jobstart.spider.TalentJob51Search;
import cn.jobstart.spider.downloader.request.TalentJob51Request;
import cn.jobstart.spider.model.Job51Json;
import cn.jobstart.spider.model.TalentJob51;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;

import java.util.List;

/**
 * @author sunke
 * @version 1.0
 * @Description 51job 人才
 * @date 2023-09-18 15:35
 */
@Data
@Slf4j
public class TalentJob51PageProcessor extends AbstractPageProcessor<TalentJob51Search> {



    private Job51Util job51Util;


    @Override
    public int getChannelFlag() {
        return TalentSubscriptionTaskChannelEnum.JOB51.getType().intValue();
    }


    /**
     * process the page, extract urls to fetch, extract the data and store
     *
     * @param page page
     */
    @Override
    public void process(Page page) {


        //{
        //    "code": "6",
        //    "msg": "登录信息已失效，请重新登录",
        //    "data": null
        //}

        String jsonStr = page.getRawText();

        log.info("jsonStr:" + jsonStr);
        JSONObject jsonObject = null;

        Job51Json job51Json = null;

        try {
            // jsonStr=jsonStr.replace("\\\"","'");


            //  jsonStr= StringEscapeUtils.unescapeJava(jsonStr);

            //  log.info("jsonStr2:" + jsonStr);

            job51Json = JSONObject.parseObject(jsonStr, Job51Json.class);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        if (StringUtil.isNullOrEmpty(job51Json)) {

            page.getResultItems().put("talentList", null);
            page.getResultItems().put("talentSubscriptionTaskId", getTalentSubscriptionTaskId());

            return;
        }


        String code = job51Json.getCode();
        //说明过期token 重新生成一个token
        if (!"0".equalsIgnoreCase(code)) {
            job51Util.removeToken();
           // job51Util.setAuthorization(null,-1);

            job51Util.initAuthorization();
            page.setSkip(true);
            // job51TalentSearch.setAuthorization(job51Util.getAuthorization());
            addTargetRequest(page, getSearch());
            return;
        }
        int currentPage = 1;
        int totalPage = 0;

        if (StringUtil.isNotObjEmpty(job51Json.getData())) {

            JSONObject dataJson = JSONObject.parseObject(job51Json.getData().toString());
            String dataArray = StringUtil.delHTMLTag(dataJson.getString("list"));


            currentPage = dataJson.getIntValue("pageNo");
            totalPage = dataJson.getIntValue("totalPages");

            List<TalentJob51> talentList = JSONArray.parseArray(dataArray, TalentJob51.class);

            //  page.getResultItems().put("list",dataJson.getJSONArray("list"));

            page.getResultItems().put("talentList", talentList);
            page.getResultItems().put("talentSubscriptionTaskId", getTalentSubscriptionTaskId());

        }





        //说明没有超过要爬数据，
        if(currentPage < totalPage && totalPage>0 &&  currentPage * getSearch().getPageSize() < getPageCount()){

            currentPage=currentPage + 1;
            getSearch().setPageIndex(currentPage);

            addTargetRequest(page, getSearch());
        }


/*
        if (currentPage > totalPage || totalPage == 0 ||  currentPage * getSearch().getPageSize() > getPageCount()) {
            page.getResultItems().put("talentList", null);
            page.getResultItems().put("talentSubscriptionTaskId", getTalentSubscriptionTaskId());
            return;
        }*/


    }


    private void addTargetRequest(Page page, TalentJob51Search job51TalentSearch) {

        try {
            TalentJob51Request request = new TalentJob51Request();
            request.build(job51TalentSearch);
            request.addHeader("Authorization", job51Util.getAuthorization());
            request.addHeader("Cookie", job51Util.getCookie());
            page.addTargetRequest(request);
        } catch (Exception e) {

        }
    }


    /**
     * get the site settings
     *
     * @return site
     * @see Site
     */
    @Override
    public Site getSite() {


        return Site.me().setCycleRetryTimes(1000).setRetrySleepTime(1000).setRetryTimes(3).setSleepTime(1000).setTimeOut(100000).setDomain("mall.51job.com");


    }
}
